﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using WebSysCore.JSons;
using WebSysCore;
using WebSysCore.Helpers;
using HSchSchedule.Utils;

namespace HSchSchedule.AjaxPages
{
    public partial class AjaxFixTeacherTime : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod(true)]
        public static string GetTeacher()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    List<Teacher> lstTeach = DataBase.GetAllTeacher(schId);
                    List<JTeacher> lstJTeach = new List<JTeacher>();
                    foreach (Teacher t in lstTeach)
                    {
                        JTeacher jt = AssignHelp.TeacherTpJTeacher(t);
                        lstJTeach.Add(jt);
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJTeach);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
        [WebMethod(true)]
        public static string GetDay()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    List<Day> lstDay = DataBase.GetAllDay(schId);
                    List<JDay> lstJDay = new List<JDay>();
                    foreach (Day d in lstDay)
                    {
                        JDay jd = AssignHelp.DayToJDay(d);
                        lstJDay.Add(jd);
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJDay);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
        [WebMethod(true)]
        public static string GetSession()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    List<Session> lstSession = DataBase.GetAllSession(schId);
                    List<JSession> lstJSession = new List<JSession>();
                    foreach (Session s in lstSession)
                    {
                        JSession js = AssignHelp.SessionToJSession(s);
                        lstJSession.Add(js);
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJSession);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }

        [WebMethod(true)]
        public static string GetFixTeacherTime(int teacherId, int dayId)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    List<FixTeacherSession> lstFix = DataBase.GetFixOfTeacher(teacherId, dayId);
                    List<JFixTeacherSession> lstJFix = new List<JFixTeacherSession>();
                    foreach (FixTeacherSession f in lstFix)
                    {
                        JFixTeacherSession jf = AssignHelp.FixTSToJFixTS(f);
                        lstJFix.Add(jf);
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJFix);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string AddFixTeacherSession(List<JFixTeacherSession> lstJFix)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    bool ok = true;
                    List<FixTeacherSession> lstFix = new List<FixTeacherSession>();
                    foreach (JFixTeacherSession jf in lstJFix)
                    {
                        if (!ValidateHelp.ValidateFTS(jf))
                        {
                            ok = false;
                            break;
                        }
                        else
                        {
                            FixTeacherSession f = AssignHelp.JFixTSToFIxTS(jf);
                            f.ScheduleInListId = schId;
                            lstFix.Add(f);
                        }
                    }
                    if (ok)
                    {
                        DataBase.InsertFixTeacherSession(lstFix);
                        return GUIConstants.SUCCESS_RESULT;
                    }
                    else
                    {
                        return GUIConstants.FAIL_RESULT;
                    }
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }

        [WebMethod(true)]
        public static string GetTeacherFix()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    List<TeacherFixInfo> lstTFI = DataBase.GetTeacherFix(schId);
                    List<JTeacherFixInfo> lstJTFI = new List<JTeacherFixInfo>();
                    foreach (TeacherFixInfo tfi in lstTFI)
                    {
                        JTeacherFixInfo jtfi = AssignHelp.TFIToJTFI(tfi);
                        lstJTFI.Add(jtfi);
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJTFI);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }            
        }

    }
}
